<?php
/* User controller 
** thaodx@gmail.com
*/
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Post extends CI_Controller {
	var $_tableName = 'data';
	public function submit(){
		$this->load->helper(array('social', 'url', 'permalink'));
		$this->load->model('Post_model', 'post');
		if(empty($_FILES["image"])) redirect(BASE_URL, 'location', 301);
		if(!isset($_SESSION['user']) || empty($_SESSION['user'])) redirect(BASE_URL . '/login', 'location', 301);
		$title = cleanValue($_POST['title']);
		$isError = false;
		if(round($_FILES["image"]['size']/1024) > UPLOAD_LIMIT){
			$msg = "The uploaded file is too large.";
			$isError = true;
			goto render;
		}
		$fileExt = strtolower(end(explode(".", $_FILES["image"]["name"])));
		if(!in_array($fileExt, array('jpg', 'gif', 'png', 'jpeg'))){
			$msg = "The file type <strong>$fileExt</strong> is not allowed.";
			$isError = true;
			goto render;
		}
		$fileName = md5($_FILES["image"]["name"] . time()).'.'.$fileExt;
		if($filePath = $this->post->doFileUpload($fileName, $_FILES["image"]['tmp_name'])){
			if($postId = $this->post->savePost(
				array(
					'post_title'	=>	$title,
					'user_id'		=>	$_SESSION['user']['id'],
					'post_image'	=>	$filePath,
					'post_unique'	=>	md5(time()),
					'item_type'		=>	'photo',
					'category_id'	=>	'2',
					'user_upload'	=>	'1'
				)
			)){
				$postUrl = permalink(array('text' => $title, 'id' => $postId));
			}
			$msg = "Upload successful, <a href=\"$postUrl\">click here</a> to view your tattoos.";
		}
		render:
		$data['msg'] = $msg;
		$data['isError'] = $isError;
		$this->layout->view('post_submit', $data);// Render view and layout
	}
}